package com.ssyt.ec.sys.service;

import java.util.List;

import com.ssyt.ec.common.util.DBUtil;
import com.ssyt.ec.sys.entity.MenuEntity;
import com.ssyt.ec.sys.entity.SysUserEntity;

public class SysUserService {
	public SysUserEntity login(String code, String password) {
		return (DBUtil
				.getUniqueResult(
						SysUserEntity.class,
						"select sys_user.*,sys_class.name AS sys_class__name  from sys_user,sys_role,sys_class where code =? and password=? and sys_user.state=1 and role_id=sys_role.id and sys_role.state=1 and sys_class.id=sys_user.class_id;",
						code, password)) == null ? DBUtil
				.getUniqueResult(
						SysUserEntity.class,
						"select * from sys_user,sys_role where code =? and password=? and sys_user.state=1 and role_id=sys_role.id",
						code, password)
				: DBUtil.getUniqueResult(
						SysUserEntity.class,
						"select sys_user.*,sys_class.name AS sys_class__name  from sys_user,sys_role,sys_class where code =? and password=? and sys_user.state=1 and role_id=sys_role.id and sys_role.state=1 and sys_class.id=sys_user.class_id;",
						code, password);

	}

	public int updatePwd(String newpwd, int id) {
		return DBUtil.executeUpdate(
				"update sys_user set password=? where id=?", newpwd, id);
	}

	public List<SysUserEntity> getUniqueUser(String email, String phone) {
		List<SysUserEntity> listsys = DBUtil
				.executeQuery(SysUserEntity.class,
						"select * from sys_user where phone=? or email=?",
						phone, email);
		return listsys;
	}

	public void updateUser(String email, String phone, int id) {
		DBUtil.executeUpdate("update sys_user set email=?,phone=? where id=?",
				email, phone, id);
	}

	public List<MenuEntity> listMenu(int id, int roleId) {
		return DBUtil
				.executeQuery(
						MenuEntity.class,
						"select * from sys_menu where find_in_set(id,(SELECT menu_ids FROM sys_role where id=?)) and pid=?;",
						roleId, id);
	}
}
